package com.free.mail;

import com.free.exception.SystemException;
import com.free.exception.SystemRuntimeException;
import com.free.mail.model.MailAuthenticator;
import com.free.mail.model.MailConfig;
import com.free.utils.PropertiesResourceUtils;
import com.free.utils.PropertyParseUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Properties;

public class MailConfigure {

    private static final String DEF_MAIL_CONFIG = "mail.properties";

    private static MailConfigure _instance = new MailConfigure(DEF_MAIL_CONFIG);

    private Map<String,Object> propertyMap;

    public static MailConfigure configurer(){
        return _instance;
    }

    public MailConfigure(String path)  {
        try{
            path = StringUtils.isEmpty(path)?DEF_MAIL_CONFIG:path;
            config(path);
        }catch (Throwable t){
            throw new SystemRuntimeException(t);
        }
    }

    private void config(String path) throws SystemException {
        Properties properties = PropertiesResourceUtils.load(path);
        Map<String,Object> params = PropertiesResourceUtils.readToMap(properties);
        if(params == null && params.size() > 0){
            propertyMap = params;
        }
    }

    public MailConfig getMailConfig() throws SystemException {
        return MailConfigure.MailConfigProperty.getConfig(propertyMap);
    }

    public MailAuthenticator getAuthenticator() throws SystemException {
        return MailConfigure.MailConfigProperty.getAuthenticator(propertyMap);
    }

    public Properties getMailProperties() {
        return MailConfigure.MailConfigProperty.getProperties(propertyMap);
    }

    public static class MailConfigProperty {

        // 协议
        private static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";

        // host
        private static final String MAIL_SMTP_HOST = "mail.smtp.host";

        // port
        private static final String MAIL_SMTP_PORT = "mail.smtp.port";

        // auth
        private static final String MAIL_SMTP_AUTH = "mail.smtp.auth";

        // debug
        private static final String MAIL_DEBUG = "mail.debug";

        // account
        private static final String MAIL_SMTP_ACCOUNT = "mail.smtp.account";

        // password
        private static final String MAIL_SMTP_PASSWD = "mail.smtp.passwd";

        public static MailConfig getConfig(Map<String,Object> properties) throws SystemException
        {
            MailConfig config = new MailConfig();
            PropertyParseUtils.set(config,"protocol",properties.get(MAIL_TRANSPORT_PROTOCOL),"");
            PropertyParseUtils.set(config,"port",properties.get(MAIL_SMTP_PORT),"");
            PropertyParseUtils.set(config,"host",properties.get(MAIL_SMTP_HOST),"");
            PropertyParseUtils.set(config,"auth",properties.get(MAIL_SMTP_AUTH),false);
            PropertyParseUtils.set(config,"debug",properties.get(MAIL_DEBUG),false);
            PropertyParseUtils.set(config,"account",properties.get(MAIL_SMTP_ACCOUNT),"");
            PropertyParseUtils.set(config,"passwd",properties.get(MAIL_SMTP_PASSWD),"");
            return config;
        }

        public static MailAuthenticator getAuthenticator(Map<String,Object> properties) throws SystemException {
            MailAuthenticator authInfo = new MailAuthenticator(
                        PropertyParseUtils.getWithDef(properties.get(MAIL_SMTP_ACCOUNT),""),
                        PropertyParseUtils.getWithDef(properties.get(MAIL_SMTP_PASSWD),"")
                    );
            return authInfo;
        }

        public static Properties getProperties(Map<String,Object> params)
        {
            Properties properties = new Properties();
            properties.putAll(params);
            return properties;
        }

    }
}
